// pages/me/setting/setting.js
import Util from '../../../../utils/utils';
import User from '../../../../utils/user';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo:{
      uid:0
    },
    initUser:true,
    openLogin:true,
    version:Util.version,
    set_pwd:false,
    repwdInput:false,
    pwdInput:false,
    pwdSetInput:false,
    need_pwd:false,
  },
  initPage: function(options) {
    let self = this;
    let openRePwd = false;
    if(options.hasOwnProperty('repwd')){
      openRePwd = true;
    }
    if(openRePwd === true){
      setTimeout(function () {
        self.setData({
          repwdInput:true
        });
      },300);
    }
    self.getUserInfo(function (userInfo) {
      self.setData({
        userInfo:userInfo,
        need_pwd:userInfo['common']['pass_noneed'],
        set_pwd:userInfo['common']['pay_pass_seted'],
      });
    },function (result) {
      self.setData({
        initUser:false,
        openLogin:true
      });
    });
  },
  onSwitchChange:function (res) {
    this.setNeedPwd(res.detail);
  },
  onConfirmPwdInput:function(res){
    let self = this;
    if(res.type === 'confirm'){
      Util.request({
        url:'s/userinfo/SetPassNoneed',
        data:{
          pass_noneed:1,
          pwd:res.detail.pwd
        },
        handle:true,
        callback:function (result) {
          if(result.status === 'ok'){
            Util.msg('设置成功！');
            self.setData({
              need_pwd:true
            });
          }else{
            Util.msg(result.msg);
          }
        }
      });
    }
  },
  onConfirmRePwdInput:function(res){
    let self = this;
    if(res.type === 'confirm'){
      Util.request({
        url:'s/userinfo/SetPassNoneed',
        data:{
          pass_noneed:1,
          pwd:res.detail.pwd
        },
        handle:true,
        callback:function (result) {
          if(result.status === 'ok'){
            Util.msg('设置成功！');
            self.setData({
              need_pwd:true
            });
          }else{
            Util.msg(result.msg);
          }
        }
      });
    }
  },
  switchChange:function () {
    this.setNeedPwd(this.data.need_pwd);
  },
  setNeedPwd:function (flag) {
    let self = this;
    // console.log(flag);
    // return false;
    if(flag === false){
      Util.request({
        url:'s/userinfo/SetPassNoneed',
        data:{
          pass_noneed:2,
          pwd:''
        },
        handle:true,
        callback:function (result) {
          if(result.status === 'ok'){
            Util.msg('设置成功！');
            self.setData({
              need_pwd:false
            });
          }else{
            Util.msg(result.msg);
          }
        }
      });
    }else{
      self.setData({
        pwdInput:true
      });
      let com = this.selectComponent('#pwdBox');
      com.getFocus();
    }
  },
  setPwd:function(){
    let self = this;
    if(this.data.set_pwd === true){
      self.setData({
        repwdInput:true
      });
    }else{
      self.setData({
        pwdSetInput:true
      });
      let com = this.selectComponent('#setPwdBox');
      com.getFocus();
    }
  },
  onConfirmSetPwdInput:function(res){
    let self = this;
    if(res.type === 'confirm'){
      Util.request({
        url:'s/userinfo/SetPassNoneed',
        data:{
          pass_noneed:1,
          pwd:res.detail.pwd
        },
        handle:true,
        callback:function (result) {
          if(result.status === 'ok'){
            Util.msg('设置成功！');
            self.setData({
              set_pwd:true
            });
            User.getUserInfoByNet(function (result) {

            });
          }else{
            Util.msg(result.msg);
          }
        }
      });
    }
  },
});
